package xdTool

import sysUtil.JsoupUtil
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdInterface.XdController

class XdWidgetEchartController extends XdController {


    @Override
    def detail() {
        return null
    }

    @Override
    def del() {
        return null
    }

    /**
     * 保存并发布最终Option内容
     * @return
     */
    @Override
    def edit() {
        def result = ResultJson.resultJson()
        try {
            def panelId = Long.parseLong(params?.widgetPanelId)
            XdWidgetPanel panel = XdWidgetPanel.get(panelId)

            def xmlNode = "prodoption"
            if(!panel){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {

                def file = SysCommUtil.getEchartTempleteXMLFile(panelId)
                def xml =  file.getText()
                def jsoup = JsoupUtil.getJsoupInstanceFromDom(xml)
                def tempOption = SysCommUtil.getTempEchartOptByXml(xml)
                jsoup.selectFirst(xmlNode).html(tempOption.toString(2))
                file.write(jsoup.toString())
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    @Override
    def save() {
        def result = ResultJson.resultJson()
        try {
            def panelId = Long.parseLong(params?.widgetPanelId)
            XdWidgetPanel panel = XdWidgetPanel.get(panelId)

            def xmlNode = params.xmlNode
            if(!panel){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {

                def file = SysCommUtil.getEchartTempleteXMLFile(panelId)
                def xml =  file.getText()
                def  jsoup = JsoupUtil.getJsoupInstanceFromDom(xml)
                //println SysCommUtil.parseObj2Json(params."${xmlNode}").toString(2)
                jsoup.selectFirst(xmlNode).html( params."${xmlNode}" )
                file.write(jsoup.toString())
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    /**
     * 更新系列配置：新增、删除
     * @return
     */
    @Override
    def update() {
        def result = ResultJson.resultJson()
        try {
            def panelId = Long.parseLong(params?.widgetPanelId)
            XdWidgetPanel panel = XdWidgetPanel.get(panelId)
            def flag = params.flag?:"add"
            def xmlNode = params.xmlNode?:"diyseries"
            def seriesValue = params?.seriesValue
            def seriesName = params?.seriesName
            if(!panel){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {

                def file = SysCommUtil.getEchartTempleteXMLFile(panelId)
                def xml =  file.getText()
                def  jsoup = JsoupUtil.getJsoupInstanceFromDom(xml)
                def  diySeriesDom = jsoup.selectFirst(xmlNode)
                def seriesJson = SysCommUtil.parseObj2Json(diySeriesDom.html())
                if(["add","update"].contains(flag)){
                    seriesJson.put(seriesName,  SysCommUtil.parseObj2Json(seriesValue) )
                }else{// if(flag =="del")
                    seriesJson.remove(seriesName)
                }

                diySeriesDom.html( seriesJson.toString(2) )
                file.write(jsoup.toString())
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                result.put(ResultJson.DATA.name,seriesJson)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    /**
     * echart 交互事件
     */
    def echartAction(){
        def result = ResultJson.resultJson()
        try {
            def panelId = Long.parseLong(params?.widgetPanelId)
            XdWidgetPanel panel = XdWidgetPanel.get(panelId)
            def flag = params.flag?:"add"
            def xmlNode = params.xmlNode?:"action"
            def pName = params?.pName
            def pNameValue = params?.pNameValue
            if(!panel){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {

                def file = SysCommUtil.getEchartTempleteXMLFile(panelId)
                def xml =  file.getText()
                def  jsoup = JsoupUtil.getJsoupInstanceFromDom(xml)
                def  diySeriesDom = jsoup.selectFirst(xmlNode)
                //println "diySeriesDom?.html():${diySeriesDom?.html()} , ${diySeriesDom?.html().length()}"
                def seriesJson = SysCommUtil.parseObj2Json(diySeriesDom?.html())
                if(["add","update"].contains(flag)){
                    seriesJson.put(pName,pNameValue  )
                }else{// if(flag =="del")
                    seriesJson.remove(pName)
                }

                diySeriesDom.html( seriesJson.toString(2) )
                file.write(jsoup.toString())
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                result.put(ResultJson.DATA.name,seriesJson)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    /**
     * 获取单个系列配置
     * @return
     */
    def getDiySeries() {
        def result = ResultJson.resultJson()
        try {
            def panelId = Long.parseLong(params?.widgetPanelId)
            XdWidgetPanel panel = XdWidgetPanel.get(panelId)
            def xmlNode =  "diyseries"
            def seriesName = params?.seriesName
            if(!panel){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {

                def file = SysCommUtil.getEchartTempleteXMLFile(panelId)
                def xml =  file.getText()
                def  jsoup = JsoupUtil.getJsoupInstanceFromDom(xml)
                def  diySeriesDom = jsoup.selectFirst(xmlNode)
                def seriesJson = SysCommUtil.parseObj2Json(diySeriesDom.html())

                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                result.put(ResultJson.DATA.name,seriesJson.get(seriesName))
                result.put(ResultJson.FORM_ITEM.name,jsoup.selectFirst("seriesform").html() )
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    @Override
    def add() {
        return null
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def getOne() {
        def result = ResultJson.resultJson()
        try {
            def panelId = Long.parseLong(params?.widgetPanelId)
            XdWidgetPanel panel = XdWidgetPanel.get(panelId)
            if(!panel){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {

                def file = SysCommUtil.getEchartTempleteXMLFile(panelId)
                def xml =  file.getText()

                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                result.put(ResultJson.DATA.name,xml)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    @Override
    def query() {
        return null
    }

    @Override
    def delete() {
        return null
    }
}
